{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模块使用2：lxml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['http://123.45.44.11/hongloumeng']\n"
     ]
    }
   ],
   "source": [
    "from lxml import etree\n",
    "data = \"\"\"\n",
    "<bookstore>\n",
    "  <book category=\"COOKING\">\n",
    "    <title lang=\"en\">西游记</title>\n",
    "    <author>吴承恩</author>\n",
    "    <year>2005</year>\n",
    "    <price>30.00</price>\n",
    "  </book>\n",
    "  <book category=\"CHILDREN\">\n",
    "    <title lang=\"en\">红楼梦</title>\n",
    "    <a href=\"http://123.45.44.11/hongloumeng\">点击阅读</a>\n",
    "    <author>曹雪芹</author>\n",
    "    <year>2005</year>\n",
    "    <price>29.99</price>\n",
    "  </book>\n",
    "  <book category=\"WEB\">\n",
    "    <title lang=\"en\">水浒传</title>\n",
    "    <author>施耐庵</author>\n",
    "    <author>Per Bothner</author>\n",
    "    <author>Kurt Cagle</author>\n",
    "    <author>James Linn</author>\n",
    "    <author>Vaidyanathan Nagarajan</author>\n",
    "    <year>2003</year>\n",
    "    <price>49.99</price>\n",
    "  </book>\n",
    "  <book category=\"WEB\">\n",
    "    <title lang=\"en\">三国演义</title>\n",
    "    <author>罗贯中</author>\n",
    "    <year>2003</year>\n",
    "    <price>39.95</price>\n",
    "  </book>\n",
    "</bookstore>\n",
    "\"\"\"\n",
    "\n",
    "e = etree.fromstring(data)\n",
    "# print(e.xpath(\"/bookstore/book/author/text()\"))\n",
    "print(e.xpath(\"//book[@category='CHILDREN']/a/@href\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "a = [\"a\",\"b\",\"c\"]\n",
    "print(\"\\n\".join(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
